﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.util {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.utils.*;
    import Display.MarketUI.*;
    import Display.InventoryUI.*;
    import Modules.stats.trackers.*;
    import Modules.stats.types.*;

    public class DelayedAssetLoader {

        public static const GENERIC_DIALOG_ASSETS:String = "assets/dialogs/ASwingAssets.swf";
        public static const QUEST_ASSETS:String = "assets/dialogs/QuestAssets.swf";
        public static const TRAIN_ASSETS:String = "assets/dialogs/TrainDialogAssets.swf";
        public static const INVENTORY_ASSETS:String = "assets/dialogs/InventoryAssets.swf";
        public static const MARKET_ASSETS:String = "assets/dialogs/MarketAssets.swf";
        public static const TABBED_MARKET_ASSETS:String = "assets/dialogs/TabbedMarketAssets.swf";
        public static const POPULATION_ASSETS:String = "assets/dialogs/PopulationAssets.swf";
        public static const BUILDABLE_ASSETS:String = "assets/dialogs/BuildablesAssets.swf";
        public static const DAILY_BONUS_ASSETS:String = "assets/dialogs/DailyBonusAssets.swf";
        public static const FOOD_BARBERPOLE:String = "assets/hud/foodPole_19x132.png";
        public static const GOODS_BARBERPOLE:String = "assets/hud/goodsPole_19x132.png";
        public static const ENERGY_ASSETS:String = "assets/dialogs/EnergyAssets.swf";
        public static const FRANCHISE_ASSETS:String = "assets/franchise/FranchiseAssets.swf";
        public static const VALENTINES_ASSETS:String = "assets/dialogs/ValentinesAssets.swf";
        public static const FRAME_MANAGER_ASSETS:String = "assets/dialogs/FrameManagerAssets.swf";
        public static const TABBED_ASSETS:String = "assets/dialogs/TabbedAssets.swf";
        public static const FLASHSALE_ASSETS:String = "assets/dialogs/FlashSaleAssets.swf";
        public static const UPGRADES_ASSETS:String = "assets/dialogs/UpgradesAssets.swf";
        public static const BIZ_UPGRADES_ASSETS:String = "assets/dialogs/BusinessUpgradesAssets.swf";
        public static const ZOO_ASSETS:String = "assets/zoo/ZooAssets.swf";
        public static const MALL_ASSETS:String = "assets/malls/MallAssets.swf";
        public static const PAYROLL_ASSETS:String = "assets/dialogs/PayrollAssets.swf";

        protected var m_assetsToPreload:Array;
        protected var m_cache:Dictionary;
        protected var m_listeners:Dictionary;
        protected var m_count:int;

        public function DelayedAssetLoader(){
            this.m_assetsToPreload = [GENERIC_DIALOG_ASSETS, MARKET_ASSETS, QUEST_ASSETS, FRAME_MANAGER_ASSETS, TRAIN_ASSETS, INVENTORY_ASSETS, BUILDABLE_ASSETS, POPULATION_ASSETS, DAILY_BONUS_ASSETS, GOODS_BARBERPOLE, ENERGY_ASSETS, FRANCHISE_ASSETS, TABBED_ASSETS, UPGRADES_ASSETS, BIZ_UPGRADES_ASSETS, ZOO_ASSETS, TABBED_MARKET_ASSETS, MALL_ASSETS, PAYROLL_ASSETS];
            super();
            this.m_cache = new Dictionary();
            this.m_listeners = new Dictionary();
        }
        protected static function makeAssetLoader(_arg1:String, _arg2:DelayedAssetLoader):DisplayObject{
            var container:* = null;
            var url:* = _arg1;
            var loader:* = _arg2;
            container = new Sprite();
            loader.get(url, function (_arg1:DisplayObject, _arg2:String):void{
                container.addChild(_arg1);
            });
            return (container);
        }

        public function startPostloading():void{
            this.m_count = this.m_assetsToPreload.length;
            var _local1:BundleLoader = new BundleLoader("assets/bundle/delay.swf");
            _local1.load(null);
            this.startPreloadingUrls(this.m_assetsToPreload);
        }
        public function startPreloadingUrls(_arg1:Array, _arg2:int=2):void{
            var _local3:String;
            for each (_local3 in _arg1) {
                if ((((this.m_cache[_local3] == null)) && ((this.m_listeners[_local3] == null)))){
                    this.m_listeners[_local3] = [];
                    this.startLoading(_local3, _arg2, this.onAssetLoaded);
                };
            };
        }
        public function isAssetLoaded(_arg1:String):Boolean{
            return (!((this.m_cache[_arg1] == null)));
        }
        protected function startLoading(_arg1:String, _arg2:int, _arg3:Function):void{
            var loader:* = null;
            var url:* = _arg1;
            var priority:* = _arg2;
            var callback:* = _arg3;
            var absoluteUrl:* = Global.getAssetURL(url);
            loader = LoadingManager.load(absoluteUrl, function (_arg1:Event):void{
                callback(url, loader);
            }, priority);
        }
        protected function onAssetLoaded(_arg1:String, _arg2:Loader):void{
            var _local5:Function;
            var _local3:DisplayObject = (_arg2.content as DisplayObject);
            if (_local3 == null){
                ErrorManager.addError(("Failed to load delayed asset: " + _arg1));
            };
            this.m_cache[_arg1] = _local3;
            this.populateGlobalVariables(_local3, _arg1);
            var _local4:Array = (this.m_listeners[_arg1] as Array);
            if (_local4 != null){
                for each (_local5 in _local4) {
                    _local5(_local3, _arg1);
                };
            };
            delete this.m_listeners[_arg1];
            this.m_count--;
            if (this.m_count == 0){
                StartupSessionTracker.perf(StatsKingdomType.LOADCOMPLETE);
            };
        }
        protected function populateGlobalVariables(_arg1:DisplayObject, _arg2:String):void{
            switch (_arg2){
                case DelayedAssetLoader.MARKET_ASSETS:
                    Catalog.setupAssets(_arg1, _arg2);
                    break;
                case DelayedAssetLoader.INVENTORY_ASSETS:
                    InventoryView.setupAssets(_arg1, _arg2);
                    break;
            };
        }
        public function get(_arg1:String, _arg2:Function):Boolean{
            var _local3:DisplayObject = this.m_cache[_arg1];
            if (_local3 != null){
                _arg2(_local3, _arg1);
                return (true);
            };
            if (this.m_listeners[_arg1] == null){
                this.m_listeners[_arg1] = [_arg2];
                this.startLoading(_arg1, LoadingManager.PRIORITY_LOW, this.onAssetLoaded);
            } else {
                (this.m_listeners[_arg1] as Array).push(_arg2);
            };
            return (false);
        }
        public function getAsset(_arg1:String):DisplayObject{
            if (this.m_cache[_arg1]){
                return ((this.m_cache[_arg1] as DisplayObject));
            };
            return (makeAssetLoader(_arg1, this));
        }

    }
}//package Classes.util 
